package com.vf.admin.miaodong.controller;

import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import static com.vf.admin.utils.StringEx.isNull;

/**
 * 上传图片
 */
@Controller
@RequestMapping("/after/uploadImage")
public class AppUploadImageController extends BaseController {

    @Autowired
    private AppUserService appUserService;

    @ResponseBody
    @RequestMapping("/uppic")
    public Result uppic(MultipartFile[] file) {
        try {
            String pic = "";
            //上传：循环获取file数组中得文件
            StringBuffer stringBuffer = new StringBuffer();
            if (file != null && file.length > 0) {
                MultipartFile files = file[0];
                if (!files.isEmpty()) {
                    //判断图片大小
                    if (files.getSize() <= 52428800) {
                        //上传阿里云oss
                        String ossUrl = appUserService.aliyunOssupload(files);
                        if (isNull(ossUrl)) {
                            return errorJSON("图片上传失败");
                        }
                        //拼接
                        stringBuffer.append(ossUrl);
                    } else {
                        return this.errorJSON("图片大小超过限制！");
                    }
                }
            } else {
                return this.errorJSON("图片不能为空");
            }
            pic = stringBuffer.toString();
            return this.sucessJSON(pic, "保存成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }

}
